/*********************************************************************
 * Módulo 3. Tutorial de desarrollo con NME. Bucle de juego
 * Programación Multimedia y Juegos
 * Autor: David Vallejo Fernández    david.vallejo@tegnix.com
 * Autor: Carlos González Morcillo   carlos.gonzalez@tegnix.com
 *
 * You can redistribute and/or modify this file under the terms of the
 * GNU General Public License ad published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * and later version. See <http://www.gnu.org/licenses/>.
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.  
 *********************************************************************/

package;

import com.eclecticdesignstudio.motion.Actuate;
import com.eclecticdesignstudio.motion.easing.Quad;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.Lib;

class Circle extends Sprite {
  
  private var _size  : Float;
  private var _blur  : Float;
  private var _alpha : Float;


  // Almacenamos el color original del círculo, y si actualmente está en negro
  private var _color : Int;
  private var _enNegro: Bool;
  public function isColorOriginal():Bool {
  	return !_enNegro;
  }
  
  
    
  public function new () {
    super ();
    
    // Generación aleatoria de info básica del círculo.
    _size = 5 + Math.random () * 35 + 20;
    _blur = 3 + Math.random () * 12;
    _alpha = 0.2 + Math.random () * 0.6;
    
    // Heredadas de Sprite.

    // ALMACENAMOS COMO CAMPO MIEMBRO DE LA CLASE EL COLOR ORIGINAL GENERADO ALEATORIAMENTE
    _color = Std.int (Math.random () * 0xFFFFFF);  
    _enNegro = false;
    
    this.graphics.beginFill (_color);     

    this.graphics.drawCircle (0, 0, _size);
    this.x = Math.random () * Lib.current.stage.stageWidth;
    this.y = Math.random () * Lib.current.stage.stageHeight;    
  }
  
  public function animate () : Void {
    var duration:Float = 1.5 + Math.random () * 4.5;
    var targetX:Float = Math.random () * Lib.current.stage.stageWidth;
    var targetY:Float = Math.random () * Lib.current.stage.stageHeight;
    
    // La animación se delega en la biblioteca Actuate.
    Actuate.tween (this, duration, { x: targetX, y: targetY }, false)
      .ease (Quad.easeOut).onComplete (animate, [ this ]);
  }

  public function pause () : Void {
    Actuate.pause(this);
  }

  public function resume () : Void {
    Actuate.resume(this);
  }


  // Dibuja el círculo con el color proporcionado
  public function fillColor () : Void {
    this.graphics.beginFill ((_enNegro)?0:_color);
    this.graphics.drawCircle (0, 0, _size);
  }
   
  public function enNegro(negro:Bool) {
    _enNegro = negro;
    this.fillColor();
  }
  

  // ¿Impacto sobre el círculo en el espacio 2D?
  public function impact (x:Float, y:Float) : Bool {
    if ((x < this.x + this._size) && (x > this.x - this._size)) {
      if ((y < this.y + this._size) && (y > this.y - this._size)) {
	return true;
      }
    }
    return false;
  }

}
